﻿<?php 

require '../frame/base.php';

$shopid = !empty($_REQUEST['shopid'])?$_REQUEST['shopid']:null;
$notice = !empty($_REQUEST['notice'])?$_REQUEST['notice']:null;
$date = !empty($_REQUEST['date'])?$_REQUEST['date']:null;
$latedate = !empty($_REQUEST['latedate'])?$_REQUEST['latedate']:null;
$blanced = !empty($_REQUEST['blanced'])?$_REQUEST['blanced']:'false';

$_REQUEST['page'] = !empty($_REQUEST['page'])?$_REQUEST['page']:'';
$_REQUEST['pagesize'] = !empty($_REQUEST['pagesize'])?$_REQUEST['pagesize']:'';

// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=20;
}
// 分页 end


if($shopid){
	if($date && $latedate && $blanced=='true'){
		$orderList = $Db->table('goodsorders')->alias('o')
		->join('car c','o.ordersid = c.ordersid')
		->where('c.state',8)->where('g.shopid',$shopid)->where('fu_kuan_datetime','<',$latedate)->where('fu_kuan_datetime','>',$date)->where('blance',true)->limit($de,$size)->select();
	}else if($blanced=='false'){
		$orderList = $Db->table('goodsorders')->alias('o')
		->join('car c','o.ordersid = c.ordersid')
		->where('goodsorders.shopid',$shopid)->where('blance',false)->limit($de,$size)->select();
	}

	$shopInfo=$Db->table('shop')->field('name,images,tel,rates')->where('shopid',$shopid)->find();
	// 订单款项
	$orderAll=array(); $i=0;

	foreach($orderList as $k=>$v){
		$cateid=$Db->table('goods')->field('xilie')->where('goodsid',$v['goodsid'])->find();
		$quanrate=$Db->table('categroy')->field('rate')->where('cateid',$cateid['xilie'])->find();

		$ordersAll[$k]['quan'] = $quanrate['rate'] * $v['totalprice'];//返券金额
		$ordersAll[$k]['yong'] = $shopInfo['rates'] * $v['totalprice'];//返佣金额

		$orderAll[$k]['numbers']=$v['numbers'];
		$orderAll[$k]['price']=$v['price'];//原价
		$orderAll[$k]['resultprice']=$v['price']-$v['coupon_price']-($shopInfo['rates'])*$v['price'];//原价
		//付款时间
		$orderAll[$k]['fu_kuan_datetime']=$v['fu_kuan_datetime'];
		//结算时间
		$orderAll[$k]['balance_datetime'] = !empty($v['shou_huo_datetime']) ? date('Y-m-d H:i:s', ( strtotime('+7 day',strtotime($v['shou_huo_datetime'])) ) ) : date('Y-m-d H:i:s', ( strtotime('+23 day',strtotime($v['fu_kuan_datetime'])) ) );

		//商品、返券、返佣金额合计	
		if($i==0){
			$totalNum=$v['totalprice'];
			$totalQuan=$ordersAll[$k]['quan'];
			$totalYong=$ordersAll[$k]['quan'];
		}else{
			$totalNum+=$v['totalprice'];
			$totalQuan+=$ordersAll[$k]['quan'];
			$totalYong+=$ordersAll[$k]['quan'];
		}

	}

	$Db->table('car')->field('ordersid')->where('')->find();
	$totalInfo = array(
		'totalNum'=>$totalNum,
		'totalQuan'=>$totalQuan,
		'totalYong'=>$totalYong,
	);
	
	$ary = array('code'=>200,' msg'=>'成功','data'=>array('orderAll'=>$orderAll,'totalInfo'=>$totalInfo,'shopInfo'=>$shopInfo));
}else if($notice){
	$notice=$Db->table('xitong')->field('notice')->find();
	$ary = array('code'=>200,' msg'=>'成功','data'=>$notice);
}else{
	$ary = array('code'=>200,' msg'=>'成功','data'=>[]);
}
echo json_encode($ary);